// facebook.js
try 
{
  

function queryFacebook(target, callback)
{
  var path = "https://graph.facebook.com/";
  path+=target;
  path+="?"+access_token+"&callback="+callback;
  var script = document.createElement('script');
  script.src = path;
  document.body.appendChild(script);        
}


function MeLoaderFacebook()
{
  window.__meLoaderFacebook = this;
  this.subscribers = [];
  this.callbacks = [];
  
  this.subscribe = function ( target )
  {
    this.subscribers.push( target );
    target.update();
  }

  this.fire = function ()
  {
    for (var i = 0; i < this.subscribers.length; i++)
        this.subscribers[i].update();
  }

  this.fire2 = function (user)
  {
    for (var i = 0; i < this.callbacks.length; i++)
        this.callbacks[i](user);
  }

  this.load2 = function(cb)
  {
    this.callbacks.push(cb);
    var controller = "window.__meLoaderFacebook";
    var target = "me";
    var callback = controller + ".loaded2";
    queryFacebook(target, callback);
  }

  this.load = function()
  {
    var controller = "window.__meLoaderFacebook";
    var target = "me";
    var callback = controller + ".loaded";
    queryFacebook(target, callback);

    var target = "me/picture";
    var callback = controller + ".pictureLoaded";
    queryFacebook(target, callback);
  }
  
  this.loaded = function(user)
  {
    
    for ( var prop in user ) if ( user.hasOwnProperty(prop) ) 
    {
      model.me[prop] = user[prop];
    }

    this.fire();
  }

  this.loaded2 = function(user)
  {
    this.fire2(user);
    var controller = "window.__meLoaderFacebook";
    var target = "me/picture";
    var callback = controller + ".pictureLoaded2";
    queryFacebook(target, callback);
  }

  this.pictureLoaded = function(src)
  {
    model.me.picture = src;
    this.fire();
  }

  this.pictureLoaded2 = function(src)
  {
    this.fire2({picture:src});
  }
}

function FriendLoader(id)
{
  
  this.id = id;
  this.callbacks = [];
  this.ready = false;
  this.dataReady = false;
  this.pictureReady = false;
  this.friend = {};
  
  this.loaded = function(user)
  {
    this.dataReady = true;
    this.ready = this.dataReady && this.pictureReady;
    for ( var prop in user ) if ( user.hasOwnProperty(prop) ) 
    {
      this.friend[prop] = user[prop];
    }

    if (this.ready) this.fire();
  }

  
  this.pictureLoaded = function(picture)
  {
    this.pictureReady = true;
    this.ready = this.dataReady && this.pictureReady;
    this.friend.picture = picture;
    if (this.ready) this.fire();
  }
  
  
  this.load = function(callback)
  {
    this.callbacks.push(callback);
    if (this.ready) 
    {
      this.fire();
    }
    else
    {
      queryFacebook(this.id, "window.friendListLoader.friends["+this.id+"].loaded");
      queryFacebook(this.id+"/picture", "window.friendListLoader.friends["+this.id+"].pictureLoaded");
    }
  }
  
  this.subscribe = function( callback )
  {
     this.callbacks.push(callback);
  }
  
  
  this.fire = function ()
  {
    for (var i = 0; i < this.callbacks.length; i++)
        this.callbacks[i](this.friend);
    this.callbacks=[];
  }
}

// загружает список друзей (без контента) в локальное хранилище 
window.friendListLoader = 
{
  callbacks: [],
  friends: {},
  
  subscribe: function( callback )
  {
     this.callbacks.push(callback);
  },
  
  fire: function (data)
  {
    for (var i = 0; i < this.callbacks.length; i++)
        this.callbacks[i](data);
  },
  
  loaded: function ( data )
  {
    if ( isLocalStorageAvailable()  )
    {
      // Пока тупо загружаем без пейджинга
      localStorage.setItem("friend-list-" + model.me.id, JSON.stringify(data.data) );
    }
    
    this.fire( data.data );
  },
  
  load: function()
  {
    queryFacebook("me/friends", "window.friendListLoader.loaded");
  },
  
  friendLoader: function(id)
  {
    if ( this.friends[id] )
      return this.friends[id];

    this.friends[id] = new FriendLoader(id);
    return this.friends[id];
  }
  
}

}
catch(e)
{
  alert(e.message);
}


/*
 * window.friendListLoader.loaded({
   "data": [
      {
         "name": "Timur Gumirov",
         "id": "503275558"
      },
      {
         "name": "Elias Taabu",
         "id": "545046705"
      },
      {
         "name": "Nikolay Borisov",
         "id": "575712334"
      },
      {
         "name": "Maxim Trukhin",
         "id": "652624559"
      },
      {
         "name": "\u0414\u044e\u043a \u0421\u0430\u043c\u043e\u0432\u0430\u0440",
         "id": "654249662"
      },
      {
         "name": "Valentina Yakovleva",
         "id": "695615991"
      },
      {
         "name": "\u041c\u0438\u0445\u0430\u0438\u043b \u0421\u0430\u0445\u0430\u0440\u043e\u0432",
         "id": "710684608"
      },
      {
         "name": "Lada Aseyeva",
         "id": "748154150"
      },
      {
         "name": "Sergey Sigitov",
         "id": "785189806"
      },
      {
         "name": "Alla Shashkina",
         "id": "791793801"
      }
   ],
   "paging": {
      "next": "https://graph.facebook.com/me/friends?access_token=AAAFPq8ObaWMBAIjXUBJzZBgrofpydihSX1MzilgPZCp50SV5Pbz5vZCYDI0hZBWzPIEbYNQgrAnKaKFwsUSPRRFtBdDncm2wNCN1dv2yKZBqYNXwJWcSJ&expires_in=5342&callback=window.friendListLoader.loaded&limit=10&offset=10&__after_id=791793801"
   }
});
 */